import axios from 'axios'
import store from '@/store'
import { Message } from 'element-ui'
const request = axios.create(
  {
    baseURL: 'https://slwl-api.itheima.net',
    timeout: 5000
  })
request.interceptors.request.use(config => {
  const token = store.state.user.token
  console.log('token', token)
  if (token) {
    config.headers.Authorization = token
  }
  return config
}, error => {
  return Promise.reject(error)
})

request.interceptors.response.use(response => {
  console.log(response.data.code)
  if (response.data.code === 200) {
    return response.data
  } else {
    console.log(response)
    Message.error(response.data.msg)
    return Promise.reject(response.data.msg)
  }
}, error => {
  return Promise.reject(error)
})

export default request
